<?php defined('SYSPATH') OR die('No direct access allowed.');

class Index_Controller extends Master_Home_Controller 
{
	
	public function index()
	{
		$config = Kohana::config('hsm');
		
		$login = new Login_Model();
		$login->initialize();
		
		$view = new View('general/content');
		
		$auth=new Auth;
		if ($auth->logged_in()) {
			$login_view = new View('login/logged');
			$this->user = $auth->get_user();

			$profiles = new Profiles_Model();
			$profile = $profiles->load($this->user->id)->current();
			
			$login_view->username = $profile['firstname'] . ' ' . $profile['lastname'];
			$login_view->picture = $profile['picture'];

			$view->login = $login_view;
		}
		else {
			$login_view = new View('login/form');
			$login_view->errors = '';
			$view->login = $login_view;
		}
		
		$top_orders = new Top_Orders_Model();
		
		$new_albums = $top_orders->get_new_albums(10);
		$new_songs = $top_orders->get_new_songs(20);
		$new_channels = $top_orders->get_new_channels(5);

		$view_new_albums = new View('top_order/new_albums');
		$view_new_albums->new_albums = $new_albums;

		$view_new_songs = new View('top_order/new_songs');
		$view_new_songs->new_songs = $new_songs;

		$view_new_channels = new View('top_order/new_channels');
		$view_new_channels->new_channels = $new_channels;
		
		$view->new_albums_view = $view_new_albums;
		$view->new_songs_view = $view_new_songs;
		$view->new_channels_view = $view_new_channels;
		
		$view_player = new View('general/player');
		$view_player->playlist = $top_orders->get_random_playlist();
		
		$view->player_view = $view_player;
		
		$this->set_title('หน้าแรก');
		$this->template->content = $view;
		$this->template->render(TRUE);
	}
}
